import { objDeepCopy,getDateString } from "@/assets/js/common.js";

let Controller = {
	created() {
		this.init();
	},
	mounted() {
		
	},
	methods: {
		init() {
			this.getAllocationList(this);		//获取列表
			this.getCard(this,20);
			this.getCard(this,10);
			this.getCard(this,undefined);
			this.getStoreOption(this); 				//获取下拉框
			this.getTypeOption(this)
		},
		//组装分页
		paginationFunction(that,obj) {
		  that.Model.paginationObject.currentPage = obj.pageIndex; // 第几页
		  that.Model.paginationObject.pageSize = obj.pageSize; //  每页数量
		  that.Model.paginationObject.pageTotal = obj.pageTotal; // 页总数
		  that.Model.paginationObject.total = obj.total; //总数
		  that.Model.paginationObject.num1 = (obj.pageIndex-1)*obj.pageSize+1//num1
		  //num2
		  if(obj.pageTotal == obj.pageIndex){
		    that.Model.paginationObject.num2 = obj.total;
		  }else{
		    that.Model.paginationObject.num2 = obj.pageIndex*obj.pageSize;
		  }
		},
		//获取卡片
		getCard(that,status){
			var apiTest = this.apiTest;
			this.$ajax({
					method: 'post',
					// url:'/api/whWarehouse/list',
					url:apiTest+'/supplier/whWarehouse/list',
					data:{
						condition:{
							businessType:80,
							status:status
						},
					}
			    }).then((res)=>{
					var data = res.data;
					if(data.status == 200){
						var result = data.result;
						if(status == 20){
							that.Model.configArr[2].count = data.result.total;
						}else if(status == 10){
							that.Model.configArr[1].count = data.result.total;
						}else{
							that.Model.configArr[0].count = data.result.total;
						}
						
					}else{
						
					}
			  });
		},
		//卡片切换 回调
		cardEmit(card){
			var params = {
				businessType:80,
				status:card.value
			}
			// var searchFromData = this.Model.searchFromData;
			this.Model.filterOption.items[2].value = card.label;
			this.getAllocationList(this,params);
		},
		//获取列表接口
		getAllocationList(that,params,page,isCard = false) {
			var apiTest = this.apiTest;
			params = params || {businessType:80};
			page = page || that.Model.page;
			this.$ajax({
					method: 'post',
					// url:apiTest+'/sale/whWarehouse/list',
					url:apiTest+'/supplier/whWarehouse/list',
					data:{
						condition:params,
						pageIndex:page.pageIndex,
						pageSize:page.pageSize,
					}
			    }).then((res)=>{
					console.log(res);
					that.Model.tableData = [];
					var data = res.data;
					if(data.status == 200){
						var obj = {};
						if(isCard){
							that.Model.cardNumberAll = data.result.total;
						}
						that.paginationFunction(that,data.result);
						var result = data.result.result;
						if(result){
							for (var i = 0; i < result.length; i++) {
								obj = objDeepCopy(result[i]);
								obj.typeDictValue = result[i].typeDict.value;
								if(result[i].outWarehouseInfo){
									obj.outWareLocation = result[i].outWarehouseInfo.theNameOfThePlace+'-'+result[i].outWarehouseInfo.descriptionOfWarehouseLocation;
									obj.intWareLocation = result[i].inWarehouseInfo.theNameOfThePlace+'-'+result[i].inWarehouseInfo.descriptionOfWarehouseLocation;
								}
								if(result[i].statusDict){
									obj.statusDictValue = result[i].statusDict.value;
								}
								that.Model.tableData.push(obj);
							}
						}
			
					}else{
						that.tips(that,'warning',data.msg)
					}
			
			  });
		},
		//获取仓库下拉框
		getStoreOption(that,params) {
			var apiTest = this.apiTest;
			params = params || {};
			this.$ajax({
					method: 'post',
					// url:apiTest+'/supplierFactoryLocation/listNoPage',
					url:apiTest+'/supplier/supplierFactoryLocation/listNoPage',
					data:{
						condition:params
					}
				}).then((res)=>{
					that.Model.storeWareData = [];
					var data = res.data;
					if(data.status == 200){
						var obj = {};
						var result = data.result;
						if(result){
							for (var i = 0; i < result.length; i++) {
								var labelStr = result[i].theNameOfThePlace+'-'+result[i].descriptionOfWarehouseLocation;
								obj = {
									label:labelStr,
									value:result[i].id
								}
								that.Model.storeWareData.push(obj);
							}
							that.Model.filterOption.items[3].data = objDeepCopy(that.Model.storeWareData);
							that.Model.filterOption.items[4].data = objDeepCopy(that.Model.storeWareData);
						}
						
					}else{
						that.tips(that,'warning',data.msg)
					}
			
			  });
		},
		//获取 调拨入库类型 下拉框
		getTypeOption(that) {
			var apiTest = this.apiTest;
			this.$ajax({
					method: 'post',
					url:apiTest+'/base/systemDictionary/getListByEntity',
					data:{
						parentId:200,
						systemDictionaryTypeId:22
					}
				}).then((res)=>{
					var data = res.data;
					that.Model.selectTypeOption = [];
					if(data.status == 200){
						var obj = {};
						var arr = [];
						var result = data.result;
						if(result){
							for (var i = 0; i < result.length; i++) {
								if(result[i].value == '调拨单'){
									
								}else{
									obj = {
										label:result[i].value,
										value:result[i].key
									}
									that.Model.selectTypeOption.push(obj);
								}
							}
							that.Model.filterOption.items[1].data = objDeepCopy(that.Model.selectTypeOption);
						}
						
					}else{
						that.tips(that,'warning',data.msg)
					}
			
			  });
		},
		//点击入库
		storageOperating(row) {
			this.$router.push({
				path:'insideAllocationStorage',
				query:{
					number:row.number
				}
			});
		},		
		//跳转到详情页
		toDetial(row) {
			this.$router.push({
				path:'insideAllocationDetail',
				query:{
					number:row.number
				}
			});
		},
		//新建页面
		newPage(row){
			this.$router.push({
				path:'insideAllocationStorageNew',
				query:{
					number:row.number
				}
			});
		},
		selectedHighlight(data) {
			// 如果选中则添加高亮样式
			if (this.Model.TableSelectData.indexOf(data.row) > -1) {
				return {
					"background-color": "#FEF6EE"
				};
				return {}
			}
		},
		warehousing(row) {
			this.Model.warehousingDialogStatus = row.status;
			this.Model.warehousingDialogFlag = true;
		},
		handleSelectionChange(val) {
			this.Model.TableSelectData = val;
		  
		},
		handleCurrentChange(val) {
			// this.Model.pager.params.pageIndex = val;
			var params = this.Model.searchFromData;
			this.Model.page.pageIndex = val;
			this.getAllocationList(this,params,this.Model.page);
		},
		handleSizeChange(val) {
			var params = this.Model.searchFromData;
			this.Model.page.pageSize = val;
			this.getAllocationList(this,params,this.Model.page);
		},
		cardClick(color) {
			this.Model.cardSelected = color;
		},
		showChild(val) {
			this.Model.searchFromData = {
				businessType:80,
				inWarehouseId:val.inWarehouseId,
				keyword:val.keyword,
				outWarehouseId:val.outWarehouseId,
				status:val.status,
				type:val.type
			}
			if(val.creatTime){
				this.Model.searchFromData.inSureTimeStart = getDateString(val.creatTime[0]);
				this.Model.searchFromData.inSureTimeEnd = getDateString(val.creatTime[1]);
			}
			
		},
		toSearch(val) {
			console.log(val)
			var params = this.Model.searchFromData;
			this.getAllocationList(this,params);
		},
		toClear(val) {
			this.Model.searchFromData = {};
			
		},
		toExport(val) {
		  console.log(val);
		},
		onTabChange(val) {
			console.log(val, this.Model.activeTab)
			switch (val) {
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
			}
		},
		tips(that, type, message) {
			let duration;
			type == 'success' ? duration = 3000 : duration = 10000;
			that.$message({
				message: message,
				type: type,
				duration: duration
			});
		},
	},
	watch:{
		$route:{
		　　　handler(newValue, oldValue) {
				this.init();
		　　　},
		　　　deep: true
		},
  	
	},
	filters:{
		objFilters:function(value){
			
		}
	}
}
export default Controller;
